﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using Model.RepositoryInterfaces;
using NHibernate;
using Type = Model.Type;

namespace DAL.Repositories
{
    public class CodeListRepository:ICodeListRepository
    {
        public void Create(CodeList entity)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    try
                    {
                        session.Save(entity);
                        transaction.Commit();
                    }
                    catch (ADOException exception)
                    {
                        throw new Exception(exception.Message);
                    }
                }
            }
        }

        public IList<AreaInterval> ReadAreaIntervals()
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                IQuery query = session.CreateQuery("from CodeList where ClassType = AreaInterval");
                var list = query.List<AreaInterval>();
                return list;
            }
        }

        public IList<PriceInterval> ReadPriceIntervals()
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                var query = session.CreateQuery("from CodeList where ClassType = PriceInterval");
                var list = query.List<PriceInterval>();
                return list;
            }
        }

        public IList<Floor> ReadFloors()
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                IQuery query = session.CreateQuery("from CodeList where ClassType = Floor");
                var list = query.List<Floor>();
                return list;
            }
        }

        public IList<RoomsNumber> ReadRoomsNumber()
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                IQuery query = session.CreateQuery("from CodeList where ClassType = RoomsNumber");
                var list = query.List<RoomsNumber>();
                return list;
            }
        }

        public IList<State> ReadStates()
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                IQuery query = session.CreateQuery("from CodeList where ClassType = State");
                var list = query.List<State>();
                return list;
            }
        }

        public IList<Type> ReadTypes()
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                IQuery query = session.CreateQuery("from CodeList where ClassType = Type order by Name");
                var list = query.List<Type>();
                return list;
            }
        }

        public IList<Facade> ReadFacades()
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                IQuery query = session.CreateQuery("from CodeList where ClassType = Facade");
                var list = query.List<Facade>();
                return list;
            }
        }

        public IList<CodeList> ReadAll()
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                var query =
                    session.CreateQuery(
                        "from CodeList where ClassType = AreaInterval or ClassType = Floor or ClassType = RoomsNumber");
                var list = query.List<CodeList>();
                return list;
            }
        }

        public CodeList Read(int id)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                var code = session.Get<CodeList>(id);
                return code;
            }
        }
    }
}
